package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //新增一个菜品
        dishMapper.insert(dish);

        //新增菜品的口味
        //获取当前新增菜品的id
        Long dishId = dish.getId();
        //获取口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //插入该菜品的口味
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }



    }

    /**
     * 根据菜品分类id查询关联菜品的数量
     * @param id
     * @return
     */
    @Override
    public Integer countByCategoryId(Long id) {
        int size= dishMapper.countByCategoryId(id);

        return size;
    }

    /**
     * 菜品分类查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> page=dishMapper.page(dishPageQueryDTO);

        PageResult pageResult=new PageResult(page.getTotal(),page.getResult());

        return pageResult;


    }

    /**
     * 根据ids批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断是否有菜品其在起售状态
        int size = dishMapper.getStatusByIds(ids);
        if(size==ids.size())
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

        //判断当前菜品是否能被删除--是否被套餐关联了
        List<Long> setmealIdsByDishIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIdsByDishIds!=null&&setmealIdsByDishIds.size()>0)
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);

        //删除菜品
        dishMapper.delete(ids);

        //删除菜品所对应的口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Transactional
    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
       Dish dish=new Dish();
       BeanUtils.copyProperties(dishDTO,dish);

       //修改菜品内容
       dishMapper.update(dish);
       //删除该菜品的所有的口味数据
       dishFlavorMapper.deleteByDishIds(Collections.singletonList(dishDTO.getId()));

       //进行菜品口味插入
       List<DishFlavor> flavors = dishDTO.getFlavors();
       if(flavors!=null&&flavors.size()>0){
           flavors.forEach(flavor->{
               flavor.setDishId(dishDTO.getId());
           });

           //向口味表里面插入数据
           dishFlavorMapper.insertBatch(flavors);
       }




    }



    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getDishWithFlavorById(Long id) {
        DishVO dishVO=new DishVO();

        //获得菜品信息
        Dish dish = dishMapper.getDishById(id);
        BeanUtils.copyProperties(dish,dishVO);
        //获取菜品口味信息
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);

        //向DishVO插入口味信息
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 菜品启用禁用
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void startOrStop(int status, Long id) {
        Dish dish=Dish.builder()
                .id(id)
                .status(status)
                .build();

        //修改状态
        dishMapper.update(dish);

        //如果菜品停售了，所关联的套餐也下架
        if(status== StatusConstant.DISABLE){
           List<Long> ids=new ArrayList<>();
           ids.add(id);
           
           //通过菜品id获取套餐id
            List<Long> setMealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
            //2.遍历套餐id
            if(setMealIds!=null&&setMealIds.size()>0){
                for (Long setMealId : setMealIds) {
                    Setmeal setmeal=Setmeal.builder()
                            .status(status)
                            .id(id)
                            .build();
                    setmealMapper.update(setmeal);
                }

            }

        }

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getDishByCategoryId(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        return dishMapper.getDishByCategoryId(dish);
    }

    /**
     * 查询起售的菜品及口味
     * @param dish
     */
    @Override
    public  List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getDishByCategoryId(dish);

        List<DishVO> dishVOList=new ArrayList<>();

        for (Dish d: dishList) {
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询其口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId((d.getId()));

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;

    }
}
